package com.fay.designPattern.principles.metrics;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/3/21 16:08
 * @Version 1.0
 */
public class Metrics {
    // Map:key for the name of api, value for api-info
    private Map<String, List<Double>> responseTimes = new HashMap<>();
    private Map<String, List<Double>> timestamps = new HashMap<>();
    private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

    public void recordResponseTime(String apiName,double responseTime) {
        responseTimes.putIfAbsent(apiName, new ArrayList<>());
        responseTimes.get(apiName).add(responseTime);
    }

    public void recordTimestamp(String apiName, double timestamp) {
        timestamps.putIfAbsent(apiName, new ArrayList<>());
        timestamps.get(apiName).add(timestamp);
    }

    public double max(List<Double> dataset) {
        return 0d;
    }
    public double avg(List<Double> dataset) {
        return 0d;
    }

    public void startRepeatedReport(long period, TimeUnit unit) {
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Gson gson = new Gson();
                Map<String, Map<String, Double>> stats = new HashMap<>();

                for (Map.Entry<String, List<Double>> entry : responseTimes.entrySet()) {
                    String apiName = entry.getKey();
                    List<Double> apiRespTimes = entry.getValue();
                    stats.putIfAbsent(apiName, new HashMap<>());
                    stats.get(apiName).put("max", max(apiRespTimes));
                    stats.get(apiName).put("avg", avg(apiRespTimes));
                }

                for (Map.Entry<String, List<Double>> entry : timestamps.entrySet()) {
                    String apiName = entry.getKey();
                    List<Double> apiTimestamps = entry.getValue();
                    stats.putIfAbsent(apiName, new HashMap<>());
                    stats.get(apiName).put("count", (double) apiTimestamps.size());
                }
                System.out.println(gson.toJson(stats));
            }
        }, 0, period, unit);
    }

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        String s1 = map.putIfAbsent("name", "zhangsan");
        String s = map.putIfAbsent("name", "lisi");
        boolean isExist = map.containsValue("zhangsan");
        map.computeIfAbsent("name", new Function<String, String>() {
            @Override
            public String apply(String s) {
                System.out.println(s);
                return s;
            }
        });
        /*System.out.println(isExist);
        System.out.println(s1);
        System.out.println(s);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey());//name
            System.out.println(entry.getValue());//zhangsan
        }*/
    }
}
